package base

import (
	"git.mycaigou.com/gfyx/common/rpc_client"
	"git.mycaigou.com/gfyx/micro-gfyx-api/http_server/controller"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/common/errcode"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/dto/base"
	"git.mycaigou.com/gfyx/micro-gfyx-api/service/base/org"
	"git.myscrm.cn/golang/stark/v4"
	"github.com/gin-gonic/gin"
	"strings"
)

type OrgController struct {
	controller.BaseController
	orgService org.OrgServiceIface
}

func NewOrgController(orgService org.OrgServiceIface) *OrgController {
	return &OrgController{
		orgService: orgService,
	}
}

// RegisterRouter
// 注册路由
func (c *OrgController) RegisterRouter(g *gin.RouterGroup) {
	// 增加/修改组织
	g.POST("/org/save", c.OrgSave)
	// 删除组织
	g.POST("/org/delete", c.OrgDelete)
	// 组织列表
	g.GET("/org/list", c.OrgList)
	// 导入数据
	g.POST("/org/import", c.OrgImport)

	g.GET("/org/area/trees", c.areaTrees)
}

func (c *OrgController) OrgSave(ctx *gin.Context) {
	req := &base.OrgItem{}
	if err := ctx.ShouldBind(req); err != nil {
		stark.Logger.Infof(ctx, "OrgSave-error: %+v", err)
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, err.Error())
		return
	}

	if req.OrgName == "" {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, "部门名称为空")
		return
	}

	if strings.Contains(req.OrgName, "-") == true {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, "部门名称不能包括字符'-'")
		return
	}

	//if req.ParentOrgId == 0 {
	//	c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, "上级部门编号为空")
	//	return
	//}

	//if len(req.AreaCodes) == 0 {
	//	c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, "关联区域为空")
	//	return
	//}

	orgId, err := c.orgService.OrgSave(ctx, req)
	if err != nil {
		stark.Logger.Infof(ctx, "OrgSave-err: %+v, req: %+v", err, req)
		grpcErr := rpc_client.GetGrpcError(err)
		c.Fail(ctx, grpcErr.GetCode(), grpcErr.GetMessage())
		return
	}

	c.Success(ctx, map[string]interface{}{
		"id": orgId,
	})
}

func (c *OrgController) OrgDelete(ctx *gin.Context) {
	req := &base.OrgDeleteParams{}
	if err := ctx.ShouldBind(req); err != nil {
		stark.Logger.Infof(ctx, "OrgDelete-error: %+v", err)
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, err.Error())
		return
	}

	if req.OrgId == 0 {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, "部门编号为空")
		return
	}

	orgId, err := c.orgService.OrgDelete(ctx, req)
	if err != nil {
		stark.Logger.Infof(ctx, "OrgDelete-err: %+v, req: %+v", err, req)
		grpcErr := rpc_client.GetGrpcError(err)
		c.Fail(ctx, grpcErr.GetCode(), grpcErr.GetMessage())
		return
	}

	c.Success(ctx, map[string]interface{}{
		"id": orgId,
	})
}

func (c *OrgController) OrgList(ctx *gin.Context) {
	list, err := c.orgService.OrgList(ctx)
	if err != nil {
		stark.Logger.Infof(ctx, "OrgList-err: %+v", err)
		grpcErr := rpc_client.GetGrpcError(err)
		c.Fail(ctx, grpcErr.GetCode(), grpcErr.GetMessage())
		return
	}

	c.Success(ctx, map[string]interface{}{
		"list": list,
	})
}

func (c *OrgController) OrgImport(ctx *gin.Context) {
	param := &base.OrgImportParam{}
	if err := ctx.ShouldBind(param); err != nil {
		stark.Logger.Infof(ctx, "OrgImport-error: %+v", err)
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, err.Error())
		return
	}
	if param.FilePath == "" {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, "文件路径为空")
		return
	}
	resp, err := c.orgService.OrgImport(ctx, param)
	if err != nil {
		stark.Logger.Info(ctx, "OrgImport-failed: "+err.Error())
		c.Fail(ctx, errcode.COMMON_ERROR, err.Error())
		return
	}

	c.Success(ctx, resp)
}

func (c *OrgController) areaTrees(ctx *gin.Context) {
	areaTrees, err := c.orgService.AreaTrees(ctx)
	if err != nil {
		stark.Logger.Infof(ctx, "areaTrees-err: %+v", err)
		grpcErr := rpc_client.GetGrpcError(err)
		c.Fail(ctx, grpcErr.GetCode(), grpcErr.GetMessage())
		return
	}
	c.Success(ctx, map[string]interface{}{
		"list": areaTrees,
	})
}
